package com.tanhua.admin.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.tanhua.admin.mapper.LogMapper;
import com.tanhua.model.pojo.Log;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class LogListener {

    @Resource
    private LogMapper logMapper;


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    name = "queue.tanhua.log",
                    durable = "true"
            ),
            exchange = @Exchange(
                    name = "tanhua.log.exchange",
                    type = ExchangeTypes.TOPIC
            ),
            key = "log.*"
    ))
    public void logMessage(String message) {
        //Map map = JSON.parseObject(message, Map.class);
        JSONObject jsonObject = JSON.parseObject(message);

        Log log = new Log();
        //log.setUserId(Long.parseLong(map.get("userId").toString()));
        log.setUserId(jsonObject.getLong("userId"));
        log.setLogTime(jsonObject.getString("logTime"));
        log.setType(jsonObject.getString("type"));

        logMapper.insert(log);

    }
}
